package com.hdl.servlet.order;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hdl.entity.HdlUser;
import com.hdl.entity.MallOrder;
import com.hdl.service.MallOrderService;
import com.hdl.service.impl.MallOrderServiceImpl;

/**
 * ReceiveServlet 签收订单
 * @author Administrator
 *
 */
@WebServlet("/receieve.let")
public class ReceiveServlet extends HttpServlet{
	
	private MallOrderService service = new MallOrderServiceImpl();

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//获取参数
		String mallOrderId = req.getParameter("mallOrderId");
		ObjectMapper mapper = new ObjectMapper();
		Map<String,Object> map = new HashMap<String,Object>();
		HdlUser user = (HdlUser) req.getSession().getAttribute("user");
		
		try {
			//调用业务逻辑层方法
			service.update(mallOrderId,4);
		}catch(Exception e) {
			e.printStackTrace();
			map.put("code", 1);
			map.put("msg", "订单状态值修改失败，请刷新页面");
			mapper.writeValue(resp.getOutputStream(), map);
			return;
		}
		
		
		List<MallOrder> list = service.findAllOrderByUserId(user.getHdlUserId(), 3);//待收货
		if(list.size() == 0) {
			map.put("code", 2);
			map.put("msg", "亲亲您目前已经没有待收货的订单了喲");
			mapper.writeValue(resp.getOutputStream(), map);
			return;
		}
		
		//写回消息和数据
		map.put("code", 3);
		map.put("msg", "订单状态值修改成功");
		map.put("list", list);
		mapper.writeValue(resp.getOutputStream(), map);
		
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
}
